package project.ccso.top.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import project.ccso.top.bean.GridPageBean;
import project.ccso.top.bean.TwoThreeCategory;
import project.ccso.top.dao.ThreeCategoryDao;
import project.ccso.top.dao.TwoCategoryDao;
import project.ccso.top.dao.impl.ThreeCategoryDaoImpl;
import project.ccso.top.dao.impl.TwoCategoryDaoImpl;
import project.ccso.top.entity.OneCategory;
import project.ccso.top.entity.ThreeCategory;
import project.ccso.top.entity.TwoCategory;
import project.ccso.top.enumerate.OpenStatus;
import project.ccso.top.service.TwoCategoryService;
import project.ccso.top.util.DateUtil;

/**
 * 二级分类Service的实现类
 * @author aibinxiao
 * @date 2018年8月23日 下午12:02:43
 */
public class TwoCategoryServiceImpl implements TwoCategoryService {
	// 日志
	private static Logger logger = LoggerFactory.getLogger(TwoCategoryServiceImpl.class);
	// 注入TwoCategoryDao
	private TwoCategoryDao twoCategoryDao = new TwoCategoryDaoImpl();
	// 注入ThreeCategoryDao
	private ThreeCategoryDao threeCategoryDao = new ThreeCategoryDaoImpl();
	private TwoCategory twoCategory = null;
	
	@Override
	public GridPageBean<TwoCategory> findByPage(String pageNoStr, String pageSizeStr, String id, String name, String code,
			String oneCategoryId, String startDate, String endDate) throws Exception {
		// TODO Auto-generated method stub
		Integer pageNum = 1;
		Integer pageSize = 10;
		if(!StringUtils.isEmpty(pageNoStr) && !StringUtils.isEmpty(pageSizeStr)){
			boolean flag = true;
			try {
				pageNum = Integer.valueOf(pageNoStr);
				pageSize = Integer.valueOf(pageSizeStr);
			} catch (Exception e) {
				flag = false;
			}
			if(!flag) {
				throw new Exception("参数page或rows不是int类型或者超出int范围！");
			}
		}
		int records = twoCategoryDao.countByParams(id, name, code,oneCategoryId,startDate, endDate);
		GridPageBean<TwoCategory> result = new GridPageBean<TwoCategory>(pageNum, records, pageSize);
		//查询分页后的具体记录
		if(records <= result.getPageSize()){
			List<TwoCategory> rows =twoCategoryDao.findPageByParams(id,name,code,oneCategoryId,startDate,endDate,result.getStartIndex(),records);
			result.setRows(rows);
		} else{
			List<TwoCategory> rows = twoCategoryDao.findPageByParams(id,name,code,oneCategoryId,startDate,endDate,result.getStartIndex(),result.getPageSize());
			result.setRows(rows);
		}
		result.setPage(pageNum);
		result.setRecords(records);
		result.setTotal(result.getTotal());
		return result;
	}

	@Override
	public TwoCategory findCate(int id) {
		// TODO Auto-generated method stub
		return twoCategoryDao.findCateById(id);
	}

	@Override
	public boolean UpdateCate(String id, String code, String name, String desc, String oneCategoryId) throws Exception {
		// TODO Auto-generated method stub
		if(StringUtils.isEmpty(name)) {
			throw new Exception("参数name为NULL或空字符串！");
		}
		return twoCategoryDao.UpdateCateById(id,code,name,desc,oneCategoryId);
	}
	
	/*// 1.根据一级分类id获取所有的二级、三级分类的Service实现类
	@Override
	public List<TwoThreeCategory> findTwoThreeCategory(Integer oneCategoryId) {
		// 声明一个二级、三级分类bean的大集合
		List<TwoThreeCategory> listTwoThreeCategory = new ArrayList<TwoThreeCategory>();
		if (oneCategoryId != null){
			// 首先获取所有的二级分类
			List<TwoCategory> listTwoCategory = twoCategoryDao.findTwoCategoryByOneId(oneCategoryId);
			for(int i=0;i<listTwoCategory.size();i++){
				// 获取到二级分类的Id
				Integer twoCategoryId = listTwoCategory.get(i).getId();
				// 根据指定二级分类Id获取到三级分类集合
				List<ThreeCategory> listThreeCategory = threeCategoryDao.findThreeCategoryByTwoId(twoCategoryId);
				// 生命一个二级、三级分类bean
				TwoThreeCategory twoThreeCategory = new TwoThreeCategory();
				// 设置bean信息
				twoThreeCategory.setTwoId(listTwoCategory.get(i).getId());
				twoThreeCategory.setTwoCode(listTwoCategory.get(i).getCode());
				twoThreeCategory.setTwoName(listTwoCategory.get(i).getName());
				twoThreeCategory.setTwoDesc(listTwoCategory.get(i).getDesc());
				twoThreeCategory.setOneCategoryId(listTwoCategory.get(i).getOneCategoryId());
				twoThreeCategory.setTwoAlterTime(listTwoCategory.get(i).getAlterTime());
				twoThreeCategory.setListThreeCategory(listThreeCategory);
				listTwoThreeCategory.add(twoThreeCategory);
			}
		}
		return listTwoThreeCategory;
	}

	// 2.根据一级分类id获取所有的二级分类的Service实现类
	@Override
	public List<TwoCategory> findTwoCateByOneId(Integer oneCategoryId) {
		List<TwoCategory> listTwoCategory = null;
		if (oneCategoryId != null){
			listTwoCategory = twoCategoryDao.findTwoCategoryByOneId(oneCategoryId);
		}
		return listTwoCategory;
	}

	// 3.分页条件查询指定一级分类下的二级分类Service实现类
	@Override
	public GridPageBean<TwoCategory> findByPage(String pageNo, String pageSize, String id, String name, String code,
			String startDate, String endDate, String oneCategoryId) {
		Integer pNo = 1;
		Integer pSize = 10;
		if(!StringUtils.isEmpty(pageNo) && !StringUtils.isEmpty(pageSize)){
			pNo = Integer.valueOf(pageNo);
			pSize = Integer.valueOf(pageSize);
		}
		int records = twoCategoryDao.countByParams(id, name, code, startDate, endDate, oneCategoryId);
		GridPageBean<TwoCategory> result = new GridPageBean<TwoCategory>(pNo, records, pSize);
		//查询分页后的具体记录
		if(records <= result.getPageSize()){
			List<TwoCategory> rows = twoCategoryDao.findPageByParams(id,name,code,startDate,endDate,oneCategoryId,result.getStartIndex(),records);
			result.setRows(rows);
		} else{
			List<TwoCategory> rows = twoCategoryDao.findPageByParams(id,name,code,startDate,endDate,oneCategoryId,result.getStartIndex(),result.getPageSize());
			result.setRows(rows);
		}
		result.setPage(pNo);
		result.setRecords(records);
		result.setTotal(result.getTotal());
		return result;
	}

	// 4.根据ID查询二级分类Service实现类
	@Override
	public TwoCategory findById(String idStr) {
		TwoCategory twoCategory = null;
		if(!StringUtils.isEmpty(idStr)){
			Integer id = Integer.valueOf(idStr);
			twoCategory = twoCategoryDao.findById(id);
		}
		return twoCategory;
	}

	// 5.保存或更新三级分类信息Service实现类
	@Override
	public void save(String idStr, String code, String name, String desc, String oneCategoryIdStr) throws Exception {
		TwoCategory twoCategory = new TwoCategory();
		// 如果id不为空则是修改更新
		if(!StringUtils.isEmpty(idStr)){
			Integer id = Integer.valueOf(idStr);
			twoCategory = twoCategoryDao.findById(id);
		}else{
			// id为空则是添加新的三级分类
			twoCategory.setCreateTime(DateUtil.DateToStr(new Date()));
			twoCategory.setStatus(OpenStatus.OPEN.getStatusCode());
		}
		if(!StringUtils.isEmpty(code)){
			twoCategory.setCode(code);
		}
		if(!StringUtils.isEmpty(name)){
			twoCategory.setName(name);
		}
		if(!StringUtils.isEmpty(desc)){
			twoCategory.setDesc(desc);
		}
		if(!StringUtils.isEmpty(oneCategoryIdStr)){
			Integer oneCategoryId = Integer.valueOf(oneCategoryIdStr);
			twoCategory.setOneCategoryId(oneCategoryId);
		}
		twoCategory.setAlterTime(DateUtil.DateToStr(new Date()));
		twoCategoryDao.saveTwoCategory(twoCategory);
	}

	// 6.更新三级分类信息的启用状态Service实现类
	@Override
	public void updateStatus(String idStr, String statusStr) throws Exception {
		Integer status = null;
		Integer id = null;
		if(StringUtils.isEmpty(idStr)){
			throw new Exception("id参数不能为空");
		}else{
			id = Integer.valueOf(idStr);
		}
		if(StringUtils.isEmpty(statusStr)){
			throw new Exception("转换状态参数不能为空");
		}
		if(!StringUtils.isEmpty(statusStr)){
			status = Integer.valueOf(statusStr);
		}
		//开启
		if(OpenStatus.OPEN.getStatusCode()==status){
			twoCategoryDao.updateStauts(id, OpenStatus.OPEN.getStatusCode());
			updateAllThreeCategoryStatusByTwoId(id, OpenStatus.OPEN.getStatusCode());
		}
		//屏蔽
		if(OpenStatus.SHIELD.getStatusCode()==status){
			twoCategoryDao.updateStauts(id, OpenStatus.SHIELD.getStatusCode());
			updateAllThreeCategoryStatusByTwoId(id, OpenStatus.SHIELD.getStatusCode());
		}
	}

	*//**
	 * 6.更新二级分类下所有三级分类的状态
	 * @param id
	 * @throws Exception
	 *//*
	private void updateAllThreeCategoryStatusByTwoId(Integer id, Integer status) throws Exception {
		List<ThreeCategory> listThreeCategory = threeCategoryDao.findAllThreeCategoryByTwoId(id);
		if(listThreeCategory!=null && listThreeCategory.size()>0){
			logger.info("该二级分类下查询到["+listThreeCategory.size()+"]个三级分类,正在同步"+getTransStatus(status));
			for (ThreeCategory threeCategory : listThreeCategory) {
				threeCategory.setStatus(status);
				threeCategoryDao.saveThreeCategory(threeCategory);
			}
		}else{
			logger.info("该二级分类下未查询到三级分类");
		}
	}
	
	*//**
	 * 7.获取正在如何更新状态
	 * @param status
	 * @return
	 *//*
	public String getTransStatus(Integer status){
		String result = null;
		if(status!=null){
			if(status==0){
				result = "启用";
			}else{
				result = "屏蔽";
			}
		}
		return result;
	}*/

}
